package com.good.child.ui.adapter.ai;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.appcompat.view.menu.MenuBuilder;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
//import com.giftedcat.picture.lib.photoview.GlideImageLoader;
//import com.giftedcat.picture.lib.photoview.style.index.NumberIndexIndicator;
//import com.giftedcat.picture.lib.photoview.style.progress.ProgressBarIndicator;
//import com.giftedcat.picture.lib.photoview.transfer.TransferConfig;
//import com.giftedcat.picture.lib.photoview.transfer.Transferee;
import com.good.child.R;
import com.good.child.widgets.dialog.PreviewDialog;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.List;

import me.kareluo.ui.OptionMenu;
import me.kareluo.ui.OptionMenuView;
import me.kareluo.ui.PopupMenuView;
import me.kareluo.ui.PopupView;

public class NineGridAdapter extends CommonAdapter<String> {

    private Context context;

    OnAddPicturesListener listener;

    public NineGridAdapter(Context context, List<String> selectPath, RecyclerView rvImages) {
        super(context, R.layout.item_img, selectPath);
        this.context = context;

        selectPath.add("");
        initTransfer(rvImages);
    }

    /**
     * 设置最大图片数量
     * */
    public void setMaxSize(int maxNum) {
//        config.setMax(maxNum);
    }

    /**
     * 设置点击添加按钮的监听
     */
    public void setOnAddPicturesListener(OnAddPicturesListener listener) {
        this.listener = listener;
    }


    /**
     * 初始化大图查看控件
     */
    private void initTransfer(RecyclerView rvImages) {
//        transferee = Transferee.getDefault(context);
//        config = TransferConfig.build()
//                .setSourceImageList(getDatas())
//                .setProgressIndicator(new ProgressBarIndicator())
//                .setIndexIndicator(new NumberIndexIndicator())
//                .setImageLoader(GlideImageLoader.with(context.getApplicationContext()))
//                .setJustLoadHitImage(true)
//                .bindRecyclerView(rvImages, R.id.iv_thum);
    }

    @Override
    protected void convert(ViewHolder viewHolder, String item, final int position) {
        ImageView ivThum = viewHolder.getView(R.id.iv_thum);
        ImageView ivAdd = viewHolder.getView(R.id.iv_add);
        ImageView deleteButton = viewHolder.getView(R.id.deleteButton);
        FrameLayout ivThumLayout = viewHolder.getView(R.id.iv_thum_layout);
        if (item.equals("")) {
            //item为添加按钮
            ivThumLayout.setVisibility(View.GONE);
            ivAdd.setVisibility(View.VISIBLE);
        } else {
            //item为普通图片
            ivThumLayout.setVisibility(View.VISIBLE);
            ivAdd.setVisibility(View.GONE);
        }
        Glide.with(mContext).load(item).into(ivThum);
        ivThum.setOnClickListener(new PicturesClickListener(position));
        ivAdd.setOnClickListener(new PicturesClickListener(position));
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDatas().remove(position);
                if (getDatas().isEmpty()) {
                    getDatas().add("");
                }
                notifyDataSetChanged();
            }
        });
    }

    /**
     * 图片点击事件
     */
    private class PicturesClickListener implements View.OnClickListener {

        int position;

        public PicturesClickListener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.iv_thum:
                    //点击图片
//                    config.setNowThumbnailIndex(position);
//                    config.setSourceImageList(getDatas());
//                    transferee.apply(config).show();
                    PreviewDialog.showDialog(((FragmentActivity) context).getSupportFragmentManager(), getDatas(),false);
                    break;
                case R.id.iv_add:
                    //点击添加按钮
                    if (listener != null)
                        listener.onAdd();
                    break;
            }
        }
    }

}